package com.ruoyi.hospital.service.doctor.impl;

import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MD5Util;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.utils.doc.DocTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.doctor.DocInfoMapper;
import com.ruoyi.common.core.domain.entity.DocInfo;
import com.ruoyi.hospital.service.doctor.IDocInfoService;

/**
 * 医生信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class DocInfoServiceImpl implements IDocInfoService 
{
    @Autowired
    private DocInfoMapper docInfoMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private DocTokenService tokenService;
    /**
     * 查询医生信息
     * 
     * @param id 医生信息主键
     * @return 医生信息
     */
    @Override
    public DocInfo selectDocInfoById(Integer id)
    {
        return docInfoMapper.selectDocInfoById(id);
    }

    /**
     * 查询医生信息列表
     * 
     * @param docInfo 医生信息
     * @return 医生信息
     */
    @Override
    public List<DocInfo> selectDocInfoList(DocInfo docInfo)
    {
        return docInfoMapper.selectDocInfoList(docInfo);
    }

    @Override
    public List<DocInfo> selectDocInfoListLike(String keyWord) {
        return docInfoMapper.selectDocInfoListLike(keyWord);
    }

    /**
     * 新增医生信息
     * 
     * @param docInfo 医生信息
     * @return 结果
     */
    @Override
    public int insertDocInfo(DocInfo docInfo)
    {

        docInfo.setCreateTime(DateUtils.getNowTimeSecond());
        return docInfoMapper.insertDocInfo(docInfo);
    }

    /**
     * 修改医生信息
     * 
     * @param docInfo 医生信息
     * @return 结果
     */
    @Override
    public int updateDocInfo(DocInfo docInfo)
    {
        docInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return docInfoMapper.updateDocInfo(docInfo);
    }

    @Override
    public int updateDocInfoApp(DocInfo docInfo) {
        docInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return docInfoMapper.updateDocInfoApp(docInfo);
    }

    @Override
    public List<DocInfo> selectDocInfoListJS(DocInfo docInfo) {
        return docInfoMapper.selectDocInfoListJS(docInfo);
    }

    /**
     * 批量删除医生信息
     * 
     * @param ids 需要删除的医生信息主键
     * @return 结果
     */
    @Override
    public int deleteDocInfoByIds(Integer[] ids)
    {
        return docInfoMapper.deleteDocInfoByIds(ids);
    }

    /**
     * 删除医生信息信息
     * 
     * @param id 医生信息主键
     * @return 结果
     */
    @Override
    public int deleteDocInfoById(Integer id)
    {
        return docInfoMapper.deleteDocInfoById(id);
    }

    @Override
    public int getNum() {
        return docInfoMapper.getNum();
    }

    //********************************医生端PC*************************************************
    /**
     *  登陆
     */
    @Override
    public String login(String username, String password, String code, String uuid) {

        //验证码验证是否正确
        validateCaptcha(username, code, uuid);

        // 登录前置校验
        loginPreCheck(username, password);

        //根据用户名和密码进行验证
        DocInfo docInfo = new DocInfo();
        docInfo.setDocPhone(username);
        docInfo.setDocPassword(MD5Util.md5Second(password));
        docInfo = docInfoMapper.loginByNameAndPwd(docInfo);
        if(docInfo==null){ //无登陆信息
            throw new UserPasswordNotMatchException();
        }
        //生成token
        String token = tokenService.createToken(docInfo);
        //将token 保存到 redis中
        tokenService.setLoginUser(docInfo);
        return token;
    }

    /*
    * 用户名和密码得验证
    * */
    @Override
    public DocInfo loginByNameAndPwd(DocInfo docInfo) {
        return docInfoMapper.loginByNameAndPwd(docInfo);
    }

    @Override
    public DocInfo loginByPhone(String docPhone) {
        DocInfo docInfo = new DocInfo();
        docInfo.setDocPhone(docPhone);
        return docInfoMapper.loginByNameAndPwd(docInfo);
    }

    @Override
    public DocInfo getInfoByOpenid(String openid) {
        return docInfoMapper.getInfoByOpenid(openid);
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid)
    {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        redisCache.deleteObject(verifyKey);
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
    }
    /**
     * 登录前置校验
     * @param username 用户名
     * @param password 用户密码
     */
    public void loginPreCheck(String username, String password)
    {
        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            throw new UserNotExistsException();
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            throw new UserPasswordNotMatchException();
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            throw new UserPasswordNotMatchException();
        }

    }

}
